package com.yanqu.road.server.cmd.monopolymarket.fortress;

import com.yanqu.road.entity.enums.eMonopolyMarketResType;
import com.yanqu.road.entity.player.UserPatrons;
import com.yanqu.road.entity.union.UnionMember;
import com.yanqu.road.pb.monopolymarket.MonopolyMarketProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.patrons.PatronsModule;
import com.yanqu.road.server.manger.MonopolyMarketCommonMgr;
import com.yanqu.road.server.manger.monopolymarket.MonopolyMarketMgr;
import com.yanqu.road.server.manger.union.CrossUnionMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @Description
 * @Author cwq
 * @Data 2021/10/15 16:32
 */
@Cmd(code = GameProtocol.S_MONOPOLY_MARKET_FORTRESS_DISPATCH, desc = "边关要塞 派遣门客")
public class MonopolyMarketFortressDispatchCmd extends AbstractCommand {

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        MonopolyMarketProto.MonopolyMarketFortressUnionDispatchReqMsg req = MonopolyMarketProto.MonopolyMarketFortressUnionDispatchReqMsg.parseFrom(packet.getBytes());
        if(!MonopolyMarketMgr.canEnterSystem(player)){
            ret = GameErrorCode.E_SYSTEM_NO_OPEN;
            return;
        }
        if(!MonopolyMarketCommonMgr.isSystemOpen(MonopolyMarketMgr.group, eMonopolyMarketResType.FORTRESS.getValue())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_SYSTEM_NO_OPEN;
            return;
        }
        // 判断是否在派遣期
        if(!MonopolyMarketCommonMgr.isDispatchFortress()){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_NO_IN_DISPATCH_PERIOD;
            return;
        }
        if(!MonopolyMarketMgr.isLockJoinUnion()){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_NOT_LOCK_JOIN_UNION;
            return;
        }
        if(!MonopolyMarketMgr.canDispatchFortress(player)){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_CANNOT_DISPATCH;
            return;
        }
        // 判断城池id 是否存在
        if(!MonopolyMarketMgr.fortressResConfigMap.containsKey(req.getId())){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_CITY_ID_NO_EXIST;
            return;
        }
        UnionMember unionMember = CrossUnionMgr.getUnionMember(player.getUserId());
        if(unionMember == null){
            ret = GameErrorCode.E_MONOPOLY_MARKET_RES_FORTRESS_CANNOT_DISPATCH;
            return;
        }
        // 判断是否有这些门客
        Set<Integer> tempObjectSet = new HashSet<>();
        List<MonopolyMarketProto.MonopolyMarketFortressPatronsDispatchEntity> list = new ArrayList<>();
        for(Integer patronsId : req.getPatronsListList()) {
            // 派遣中
            UserPatrons userPatrons = player.getModule(PatronsModule.class).getUserPatrons(patronsId);
            if (userPatrons == null) {
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_OBJECT_NOT_EXIST;
                return;
            }
            if (tempObjectSet.contains(patronsId)) {
                ret = GameErrorCode.E_MONOPOLY_MARKET_RES_GRAB_OBJECT_ID_IS_DUPLICATE;
                return;
            }
            tempObjectSet.add(patronsId);
            list.add(MonopolyMarketMgr.builderMonopolyMarketFortressPatronsDispatchEntity(userPatrons));
        }

        MonopolyMarketProto.MonopolyMarketFortressUnionDispatchReqMsgToCrossReqMsg.Builder crossMsg = MonopolyMarketProto.MonopolyMarketFortressUnionDispatchReqMsgToCrossReqMsg.newBuilder();
        crossMsg.setCtsMsg(req);
        crossMsg.setUnionId(unionMember.getUnionUid());
        crossMsg.setUnionName(CrossUnionMgr.getUnionName(unionMember.getUnionUid()));
        crossMsg.addAllPatronsList(list);
        player.sendPacket(CrossProtocol.C_MONOPOLY_MARKET_FORTRESS_DISPATCH, crossMsg);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            MonopolyMarketProto.MonopolyMarketFortressUnionDispatchRespMsg.Builder builder = MonopolyMarketProto.MonopolyMarketFortressUnionDispatchRespMsg.newBuilder();
            builder.setRet(ret);
            player.sendPacket(ClientProtocol.U_MONOPOLY_MARKET_FORTRESS_DISPATCH, builder);
        }
    }

}
